﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace Admin.CYT369.Com.DAL.DbContexts
{
    public partial class AdminContext : Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<Admin.CYT369.Com.DAL.Domains.SysUser,
                                                                                                        Admin.CYT369.Com.DAL.Domains.Role,
                                                                                                        int,
                                                                                                        Admin.CYT369.Com.DAL.Domains.sys.UserClaim,
                                                                                                        Admin.CYT369.Com.DAL.Domains.sys.UserRole,
                                                                                                        Admin.CYT369.Com.DAL.Domains.sys.UserLogin,
                                                                                                        Admin.CYT369.Com.DAL.Domains.sys.RoleClaim,
                                                                                                        Admin.CYT369.Com.DAL.Domains.sys.UserToken>
    {
        public AdminContext()
        {
        }

        public AdminContext(DbContextOptions<AdminContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (Admin.CYT369.Com.Utility.SystemSettingService.Instance.admin_cyt369_com_connection_string.DBType == "SQLSERVER2008")
            {
                optionsBuilder.UseSqlServer(Admin.CYT369.Com.Utility.SystemSettingService.Instance.admin_cyt369_com_connection_string.ConnectionString).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
            }
            else
            {
                optionsBuilder.UseSqlServer(Admin.CYT369.Com.Utility.SystemSettingService.Instance.admin_cyt369_com_connection_string.ConnectionString);
            }
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            AppModelCreating(builder);
            BaseModelCreating(builder);
        }

        private void AppModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.AppPlugs>().ToTable("TB_AppPlugs");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.AppLog>().ToTable("TB_AppLog");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.AppDictionary>().ToTable("TB_AppDictionary");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.AppOperation>().ToTable("TB_AppOperation");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.SystemMenuOperation>().ToTable("TB_AppMenuOperation");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.RoleMenuOperation>().ToTable("TB_RoleMenuOperation");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.SystemMenu>(b =>
            {
                b.ToTable("TB_AppMenu");
                b.HasMany(c => c.SubMenus)
                    .WithOne(c => c.ParentMenu)
                    .HasPrincipalKey(c => c.Id)
                    .HasForeignKey(c => c.ParentID);

                b.Ignore(c => c.id);
                b.Ignore(c => c.text);
                b.Ignore(c => c.iconCls);
                b.Ignore(c => c.state);
                b.Ignore(c => c.selected);
                b.Ignore(c => c.children);
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Apps.RoleMenu>().ToTable("TB_RoleMenu").HasMany(c => c.RoleMenuOperations).WithOne(c => c.RoleMenu).HasForeignKey(c => c.RoleMenuID);

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Organization>(b => {
                b.ToTable("TB_Organization");
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Department>().ToTable("TB_Department");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.DepartmentUser>(b =>
            {
                b.ToTable("TB_DepartmentUser");
            });


            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Position>().ToTable("TB_Position");
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.PositionUser>(b =>
            {
                b.ToTable("TB_PositionUser");
            });
        }

        private void BaseModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.SysUser>(b =>
            {
                b.HasKey(u => u.Id);
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
                b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
                b.ToTable("TB_User");
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);
                b.Property(u => u.Remark).HasMaxLength(512);
                b.Ignore(u => u.LockoutEnd);

                // Replace with b.HasMany<IdentityUserClaim>().
                b.HasMany<Admin.CYT369.Com.DAL.Domains.sys.UserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
                b.HasMany<Admin.CYT369.Com.DAL.Domains.sys.UserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
                b.HasMany<Admin.CYT369.Com.DAL.Domains.sys.UserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.sys.UserClaim>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("z_tb_user_claim");
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.sys.UserRole>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("TB_RoleUser");
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.sys.UserLogin>(b =>
            {
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });
                b.ToTable("z_tb_user_login");
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.sys.UserToken>(b =>
            {
                b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name });
                b.ToTable("z_tb_user_token");
            });


            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.SysUser>(b =>
            {
                b.HasMany<Admin.CYT369.Com.DAL.Domains.sys.UserRole>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.Role>(b =>
            {
                b.HasKey(r => r.Id);
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique();
                b.ToTable("TB_Role");
                b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.Name).HasMaxLength(256);
                b.Property(u => u.NormalizedName).HasMaxLength(256);

                b.HasMany<Admin.CYT369.Com.DAL.Domains.sys.UserRole>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
                b.HasMany<Admin.CYT369.Com.DAL.Domains.sys.RoleClaim>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
            });

            modelBuilder.Entity<Admin.CYT369.Com.DAL.Domains.sys.RoleClaim>(b =>
            {
                b.HasKey(rc => rc.Id);
                b.ToTable("z_tb_role_claim");
            });
        }


    }
}
